package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.FilterParam;
import com.macro.mall.dto.SmsHomeAdvertiseDto;
import com.macro.mall.dto.UmsAdminDTO;
import com.macro.mall.mapper.PmsProductMapper;
import com.macro.mall.mapper.SmsHomeAdvertiseMapper;
import com.macro.mall.model.PmsProduct;
import com.macro.mall.model.SmsHomeAdvertise;
import com.macro.mall.model.SmsHomeAdvertiseExample;
import com.macro.mall.service.PmsProductService;
import com.macro.mall.service.SmsHomeAdvertiseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 首页广告管理Service实现类
 * Created by macro on 2018/11/7.
 */
@Service
public class SmsHomeAdvertiseServiceImpl implements SmsHomeAdvertiseService {
    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;
    @Autowired
    private PmsProductService productService;
    @Autowired
    private PmsProductMapper productMapper;

    @Override
    public int create(long storeId,SmsHomeAdvertise advertise) {
        boolean verify = verify(advertise);
        if(!verify){
            return -2;
        }
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        advertise.setStoreId(storeId);
        return advertiseMapper.insert(advertise);
    }

    @Override
    public int delete(List<Long> ids) {
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        example.createCriteria().andIdIn(ids);
        return advertiseMapper.deleteByExample(example);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        SmsHomeAdvertise record = new SmsHomeAdvertise();
        record.setId(id);
        record.setStatus(status);
        return advertiseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public SmsHomeAdvertiseDto getItem(Long id) {
        SmsHomeAdvertise smsHomeAdvertise = advertiseMapper.selectByPrimaryKey(id);
        PmsProduct product = productMapper.selectByPrimaryKey(Long.parseLong(smsHomeAdvertise.getUrl()));
        SmsHomeAdvertiseDto dto = new SmsHomeAdvertiseDto();
        BeanUtils.copyProperties(smsHomeAdvertise,dto);
        dto.setProductName(product.getName());
        dto.setProductPic(product.getPic());
        return dto;
    }

    @Override
    public int update(Long id, SmsHomeAdvertise advertise) {
        boolean verify = verify(advertise);
        if(!verify){
            return -2;
        }
        advertise.setId(id);
        return advertiseMapper.updateByPrimaryKeySelective(advertise);
    }

    @Override
    public List<SmsHomeAdvertise> list(UmsAdminDTO umsAdmin, FilterParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<SmsHomeAdvertise>  smsHomeAdvertiseList = new ArrayList<>();
        if(umsAdmin.getAdminType()!=2){
            return smsHomeAdvertiseList;
        }
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        SmsHomeAdvertiseExample.Criteria criteria = example.createCriteria();
        if(umsAdmin.getAdminType().equals(2)){
            criteria.andStoreIdEqualTo(umsAdmin.getStoreId());
        }
        if (!StringUtils.isEmpty(param.getName())) {
            criteria.andNameLike("%" + param.getName() + "%");
        }
        if (param.getType() != null) {
            criteria.andTypeEqualTo(param.getType());
        }
        if (!StringUtils.isEmpty(param.getEndTime())) {
            String startStr = param.getEndTime() + " 00:00:00";
            String endStr = param.getEndTime() + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date start = null;
            try {
                start = sdf.parse(startStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date end = null;
            try {
                end = sdf.parse(endStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (start != null && end != null) {
                criteria.andEndTimeBetween(start, end);
            }
        }
        example.setOrderByClause("sort desc");
        smsHomeAdvertiseList =  advertiseMapper.selectByExample(example);
        return  smsHomeAdvertiseList;
    }

    /**
     * 判断商品是否已审核，未审核返回-2
     * @param advertise
     * @return
     */
    private boolean verify(SmsHomeAdvertise advertise) {
        boolean numeric = StringUtils.isNumeric(advertise.getUrl());
        if (numeric) {
            boolean verify = productService.whetherVerify(Long.parseLong(advertise.getUrl()));
            return verify;
        } else {
            return false;
        }
    }
}
